package com.chen.user.controller;

import com.chen.model.dto.h5.UserRegisterDto;
import com.chen.model.dto.system.LoginDto;
import com.chen.model.vo.common.Result;
import com.chen.model.vo.common.ResultCodeEnum;
import com.chen.model.vo.h5.UserInfoVo;
import com.chen.user.service.UserService;

import com.chen.util.IpUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author cgj
 * @date 4/17/25 2:51 PM
 */
@RestController
@RequestMapping("/api/user/userInfo")
//http://localhost:8500/api/user/userInfo/register
public class UserController {
    @Autowired
    private UserService userservice;
//    http://localhost:8500/api/user/userInfo/login
    /**
     * 登录后获取用户信息
     http://localhost:8500/api/user/userInfo/auth/getCurrentUserInfo
     */
    @GetMapping("/auth/getCurrentUserInfo")
     public Result<UserInfoVo> getCurrentUserInfo(@RequestHeader(name = "token") String token){
         UserInfoVo userInfoVo=userservice.getCurrentUserInfo(token);
         return Result.build(userInfoVo,ResultCodeEnum.SUCCESS);
     }

    /**
     * 用户登录
     * @param loginDto
     * @param req
     * @return
     */
    @PostMapping("/login")
    public  Result<String> login(@RequestBody LoginDto loginDto, HttpServletRequest req){
        String token=userservice.login(loginDto,req);
        return  Result.build(token,ResultCodeEnum.SUCCESS);
    }
    /**
     * 用户注册
     * @param userRegisterDto
     * @return
     */
    @PostMapping("/register")
    public Result register(@RequestBody UserRegisterDto userRegisterDto){
        userservice.register(userRegisterDto);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }
}
